//
//  SetPasswordVC.swift
//  LeTu
//
//  Created by gy on 2024/1/3.
//

import UIKit

class SetPasswordVC: BaseViewController {
    
    private var viewModel = SetPasswordVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configUI()
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        DispatchQueue.main.async {
            self.conformBtn.layerCornerRadius(CGFloat.app_btnHeight/2)
        }
    }
    
    // MARK: - UIButtonAction
    @objc func conformBtnAction() {
        
        guard let password = passwordView.contentTextField.text, let agaginPassword = againPasswordView.contentTextField.text, password.count > 0 && agaginPassword.count > 0 else { return showMessage(message: passwordView.contentTextField.placeholder ?? "") }
        guard password == agaginPassword else { return showMessage(message: "密码不一致，请检查！".localized) }
        showIndicator()
        viewModel.setPassword(password: password) { [weak self] dialog in
            
            guard let strongSelf = self else { return }
            DispatchQueue.main.async {
                strongSelf.hideIndicator()
                strongSelf.showMessage(message: dialog.message)
                if dialog.code == 200 {
                    strongSelf.backAction()
                }
            }
            
        }
    }
    
    // MARK: - configUI
    func configUI() {
        
        navIsHidden = false
        title = "设置密码".localized
        
        view.addSubview(lineView)
        view.addSubview(passwordBackView)
        passwordBackView.addSubview(passwordLabel)
        passwordBackView.addSubview(passwordView)
        view.addSubview(againPasswordBackView)
        againPasswordBackView.addSubview(againPasswordLabel)
        againPasswordBackView.addSubview(againPasswordView)
        view.addSubview(conformBtn)

        lineView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(10)
        }
        passwordBackView.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(lineView.snp.bottom).offset(28)
        }
        passwordLabel.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.top.equalToSuperview()
        }
        passwordView.snp.makeConstraints { make in
            make.top.equalTo(passwordLabel.snp.bottom).offset(6)
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(50)
        }
        againPasswordBackView.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.top.equalTo(passwordBackView.snp.bottom).offset(24)
        }
        againPasswordLabel.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.top.equalToSuperview()
        }
        againPasswordView.snp.makeConstraints { make in
            make.top.equalTo(againPasswordLabel.snp.bottom).offset(6)
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(50)
        }
        conformBtn.snp.makeConstraints { make in
            make.left.right.equalTo(passwordBackView)
            make.top.equalTo(againPasswordBackView.snp.bottom).offset(28)
            make.height.equalTo(CGFloat.app_btnHeight)
        }
                
        conformBtn.addTarget(self, action: #selector(conformBtnAction))
        passwordView.contentTextField.delegate = self
        againPasswordView.contentTextField.delegate = self
        
    }
    private lazy var lineView: UIView = {
        let lineView = UIView()
        lineView.backgroundColor = .app_viewBackgroundColor
        return lineView
    }()
    // - 密码
    private lazy var passwordBackView: UIView = {
        let passwordBackView = UIView()
        return passwordBackView
    }()
    private lazy var passwordLabel: UILabel = {
        let passwordLabel = UILabel()
        passwordLabel.text = "新密码".localized
        passwordLabel.font = kSetPingFangMedium(15)
        passwordLabel.textColor = .app_textDarkGrayColor
        return passwordLabel
    }()
    private lazy var passwordView = LoginInputView(placeholder: "请输入密码".localized)
    // - 再次输入密码
    private lazy var againPasswordBackView: UIView = {
        let againPasswordBackView = UIView()
        return againPasswordBackView
    }()
    private lazy var againPasswordLabel: UILabel = {
        let againPasswordLabel = UILabel()
        againPasswordLabel.text = "确认密码".localized
        againPasswordLabel.font = kSetPingFangMedium(15)
        againPasswordLabel.textColor = .app_textDarkGrayColor
        return againPasswordLabel
    }()
    private lazy var againPasswordView = LoginInputView(placeholder: "请再次输入密码".localized)
    // - 确定
    private lazy var conformBtn: GradientButton = {
        let conformBtn = GradientButton()
        conformBtn.setTitle("确定".localized)
        conformBtn.gradientColors = UIColor.app_themeGradientColors
        return conformBtn
    }()

}

extension SetPasswordVC: UITextFieldDelegate {
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
}




